<?php
defined('_JEXEC') or die('Restricted access');

require_once(JPATH_COMPONENT.DS.'persistance'.DS.'DAOSong.php');
require_once(JPATH_COMPONENT.DS.'domain'.DS.'DTOSong.php');

class DAOSongImpl implements DAOSong{
	
	public function addSong($con, $dtoSong){
		if(!$this->checkSong($con, $dtoSong->getIsoCode(), $dtoSong->getId())){
			$data = new stdClass();
			$data->iso_code = $dtoSong->getIsoCode();
			$data->id = $dtoSong->getId();
			$data->name_es = $dtoSong->getNameEs();
			$data->name_eu = $dtoSong->getNameEu();
			$data->spelling = $dtoSong->getSpelling();
			$data->pronoun = $dtoSong->getPronoun();
			$data->audio_path = $dtoSong->getAudioPath();
			$data->active = $dtoSong->isActive();

			if(!$con->insertObject( '#__hizkuntzak_songs', $data)){
				echo $con->stderr();
				return false;
			}
			else 
				return true;	
		}
		else{
			
			return $this->updateSong($con, $dtoSong);
		}
		return false;
	}
	
	public function updateSong($con, $dtoSong){
		
		$sql = "UPDATE #__hizkuntzak_songs SET spelling='$dtoSong->spelling', ";
		$sql = $sql."name_es='$dtoSong->name_es', name_eu='$dtoSong->name_eu', ";
		$sql = $sql."pronoun='$dtoSong->pronoun', audio_path='$dtoSong->audioPath', ";
		$sql = $sql."active=$dtoSong->active WHERE iso_code='$dtoSong->iso_code' and id=$dtoSong->id ";

		$con->setQuery($sql);
		
		if(!$con->query()){
			$con->stderr();
			return false;
		}
		else
			return true;
	}
	
	public function checkSong($con, $lang_code, $id){
		$sql = "select * from #__hizkuntzak_songs where (iso_code='$lang_code' AND id=$id)";
		
		$con->setQuery($sql);
		
    	$row = $con->loadObject();
    	
    	if($row == null || $row==""){
    		return false;
    	}
    	return true;
	}
	
	public function getSong($con, $lang_code, $id){
		$sql = "select * from #__hizkuntzak_songs where (iso_code='$lang_code' AND id='$id')";

		$con->setQuery($sql);
		
    	$row = $con->loadObject();

    	if($row == null || $row==""){
    		return null;
    	}
    	else{
    		$song = new DTOSong();
        	
        	$song->setIsoCode($row->iso_code);
        	$song->setId($row->id);
        	$song->setNameEs($row->name_es);
			$song->setNameEu($row->name_eu);
        	$song->setSpelling($row->spelling);
        	$song->setPronoun($row->pronoun);
        	$song->setAudioPath($row->audio_path);
        	$song->setActive($row->active);

        	return $song;
    	}
	}
	
	public function getLangSongs($con, $lang_code, $active){
		$results = array();
		
		$sql = "SELECT * 
				FROM #__hizkuntzak_songs 
				WHERE #__hizkuntzak_songs.iso_code='$lang_code'";
		
		if($active){
			$sql = $sql. " AND #__hizkuntzak_songs.active=1 ";
		}
		
		$con->setQuery($sql);

    	$objectList = $con->loadObjectList();
		
        foreach($objectList as $row) {
        	
        	$song = new DTOSong();
        	
        	$song->setIsoCode($row->iso_code);
        	$song->setId($row->id);
        	$song->setNameEs($row->name_es);
			$song->setNameEu($row->name_eu);
        	$song->setSpelling($row->spelling);
        	$song->setPronoun($row->pronoun);
        	$song->setAudioPath($row->audio_path);
        	$song->setActive($row->active);
            
        	$results[] = $song;
          } 

		return $results;
	}
	
}